package cn.netjava.robo3d.device;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URL;

import cn.netjava.robo3d.RoboTank;

import com.jme.bounding.BoundingBox;
import com.jme.math.Quaternion;
import com.jme.math.Vector3f;
import com.jme.scene.Node;
import com.jme.scene.TriMesh;
import com.jme.scene.shape.Box;
import com.jme.scene.shape.Disk;
import com.jme.system.DisplaySystem;
import com.jme.util.export.binary.BinaryImporter;
import com.jmex.model.converters.FormatConverter;
import com.jmex.model.converters.ObjToJme;

/**
 * <code>TestDynamicSmoker</code>
 * 
 * @author Joshua Slack
 */
public class CaseMate extends Node {

	private DisplaySystem display;
	private Disk emitDisc; // Geomitry;

	public CaseMate(DisplaySystem display) {
		this.display = display;
		initcasemate();
	}

	public TriMesh getGeometry() {
		return emitDisc;
	}

	/**
	 * builds the trimesh.
	 * 
	 * @see com.jme.app.SimpleGame#initGame()
	 */
	private void initcasemate() {
		Node mate = new Node();
		this.setName("casemate");
		Box box = new Box("box", new Vector3f(-1, -1, -1),
				new Vector3f(1, 1, 1));
		box.setLocalTranslation(new Vector3f(0, 0, 0));
		//Tube tube = new Tube("tube", 0.5f, 0.3f, 2f, 15, 15);
		// Capsule tube=new Capsule("tube",0.5f,0.3f,2f,15,15);
		Quaternion tubequa = new Quaternion();
		tubequa.fromAngleNormalAxis((float) Math.PI / 2, new Vector3f(0, 0, 1));
		//tube.setLocalRotation(tubequa);
		//tube.setLocalTranslation(new Vector3f(-2f, 0, 0));
		// mate.attachChild(box);
		// mate.attachChild(tube);

		URL modelurl = RoboTank.class.getClassLoader().getResource(
				"cn/netjava/robo3d/model/炮塔.obj");
		FormatConverter converter = new ObjToJme();
		// Point the converter to where it will find the .mtl file from
		// converter.setProperty("mtllib",modelurl);

		// This byte array will hold my .jme file
		ByteArrayOutputStream BO = new ByteArrayOutputStream();
		try {
			// Use the format converter to convert .obj to .jme
			converter.convert(modelurl.openStream(), BO);
			TriMesh matemodel = (TriMesh) BinaryImporter.getInstance().load(
					new ByteArrayInputStream(BO.toByteArray()));
			// shrink this baby down some
			matemodel.setLocalScale(0.04f);
			matemodel.setModelBound(new BoundingBox());
			matemodel.updateModelBound();
			Quaternion qua = new Quaternion();
			qua.fromAngleAxis(0, new Vector3f(0, 1, 0));
			matemodel.setLocalRotation(qua);
			// Put her on the scene graph
			mate.attachChild(matemodel);
		} catch (IOException e) { // Just in case anything happens

			System.exit(0);
		}
		this.updateWorldBound();

		mate.setLocalTranslation(new Vector3f(0f, 0.4f, 0));
		emitDisc = new Disk("disc", 6, 6, 0.5f);
		emitDisc.setLocalTranslation(new Vector3f(2.7f, 0, 0));
		emitDisc.setCullHint(CullHint.Always);
		mate.attachChild(emitDisc);
		mate.setLocalScale(0.22f);
		Quaternion matequa = new Quaternion();
		matequa.fromAngleNormalAxis((float) Math.PI / 2, new Vector3f(0, 1, 0));
		mate.setLocalRotation(matequa);
		this.setLocalTranslation(new Vector3f(0, -0.32f, 0));
		this.attachChild(mate);

	}

}
